﻿@using GoodMan.Infrastructure.Entities.Entity;
@{
    
    Layout = "~/Views/Shared/_Main.cshtml";
    int productId = ViewBag.productiId == null ? 0 : ViewBag.productiId;
    Product product = ViewBag.product == null ? new Product() : ViewBag.product;
    ViewBag.Title = product.ProductName;
    Policy Specialpolicy = ViewBag.policy;// == null ? new Policy() : ViewBag.policy;
    int areaCode = ViewBag.AreaCode == null ? 0 : ViewBag.AreaCode;
    int type = ViewBag.Type == null ? 0 : ViewBag.Type;
    var areacodeStr = "普通商品";
    string url = "";
    url = "/shop/PrductArea?code=general&type=1";
    if (areaCode == 2)
    {
        url = "/shop/PrductArea?code=groupbuy";
        areacodeStr = "校园团购";
    }
    else if (areaCode == 3)
    {
        url = "/shop/PrductArea?code=exchange";
        areacodeStr = "积分商城";
    }
    else if (areaCode == 1)
    {
        if (type == 1)
        {
            url = "/shop/PrductArea?code=general&type=1";
            areacodeStr = "普通商品";
        }
        else if (type == 2)
        {
            url = "/shop/PrductArea?code=general&type=2";
            areacodeStr = "特别推荐";
        }
        else if (type == 3)
        {
            url = "/shop/PrductArea?code=general&type=3";
            areacodeStr = "限时特卖";
        }
    }
    
    ItemListBox<OrderDetail> orderDetails = ViewBag.saleRecord;
    ItemListBox<Appraise> appraiselist = ViewBag.appraiselist;
    DateTime hisDate=new DateTime(1970,1,1);
    double serverDate = (DateTime.Now - hisDate).TotalSeconds;
    bool IsUnderstock=ViewBag.IsUnderstock;
    int productRemain=ViewBag.ProductRemain;

    double totalAppraise = ViewBag.totalAppraise == null ? 5.0d : ViewBag.totalAppraise;
    string Satisfaction = ViewBag.Satisfaction == null ? "100%" : ViewBag.Satisfaction;
    
    int IsViewAppriaseBtn = ViewBag.IsViewAppriaseBtn == null ? 0 : ViewBag.IsViewAppriaseBtn;

    int DelAuthenticate = ViewBag.DelAuthenticate == null ? 0 : ViewBag.DelAuthenticate;
    
    int islogin = 1;
    var loginUrl = "";
    if(!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        islogin = 0;
        string tourl = System.Web.HttpContext.Current.Request.RawUrl;
        loginUrl = "/account/login?returnurl=" + System.Web.HttpContext.Current.Server.UrlEncode(tourl);
    }

    int userRole = ViewBag.userRole == null ? 1 : ViewBag.userRole;

    decimal limitprice = ViewBag.limitprice == null ? 5 : ViewBag.limitprice;
}
@section head
{
      <script type="text/javascript">

          function submitform() {
             var price = $("#price").val();//
             var buyCount=$("#buyCount").val();
             if(price*buyCount>=@limitprice)
             {
                document.forms["buyproduct"].submit();
              }
              else
              {
                if(confirm("购买金额不足"+@limitprice+"元。\r\n1.如继续购买请点击“是”，购买完成后，请到楼栋售货员处提货并付款。\r\n2.如不需继续请点击“否”后，然后添加商品到购物车，满"+@limitprice+"元后提交订单。"))
                {
                    document.forms["buyproduct"].submit();
                }
                else
                {
                    return false;
                }
              }
          }
           function submitform2() {
             document.forms["buyproduct"].submit();
           }
          function changeNum() {
              // alert(id + price);
              var num = $("#buyCount").val();
              if (num == "" || num <= 0) {
                  $("#buyCount").val("1");
                   
                  alert("数量不能为空且必须大于等于1");
              }
              else if(num>@productRemain)
              {
                    $("#buyCount").val(@productRemain);
                    alert("库存不足,仅剩"+@productRemain+"件");
              }
              
          }
           function doAppraise()
          {
              var obj=$("#doAppraise");
              var classstr= obj.attr("class").toString();
              if(classstr=="Hide")
              {
                obj.removeClass("Hide");
                obj.addClass("View");
                obj.css("display","block");
              }
              else
              {
                obj.removeClass("View");
                obj.addClass("Hide");
                obj.css("display","none");
              }
          }
//          　$(document.ready(function(){
//　　            $("#addcart").click(function(){
//　　            $.ajax({
//　　                type:'POST',
//　　                url:"/Shop/AddProductToCart",
//　　                data:{
//　　                verNames: $("#verNames").val(),
//　　                productid:$("#productid").val(),
//　　                buycount:$("#buycount").val()
//　　                },
//　　                dataType: "json"
//　　              });
//　　          });
//　　      });
//        
        
　　            function addcart()
                { 
                  var verValues ="";
                   $("input[id='verValues']:[checked]").each(function(){
                    var value=$(this).val();
                    verValues=verValues+value+",";
                   });
                  
                 $.post("/Shop/AddProductToCart",{ verNames: null,verValues: verValues,
　　                productid:$("#productid").val(),
　　                buycount:$("#buyCount").val()}, function (response){
                    $("#cart").html(response.split('^')[0]);
                    $("#shoppingcartitem").html(response.split('^')[1]);
                });
                alert("亲，加入购物车成功。");
                return false;
　　            
　　          }
　　      

      </script>
     

}
<!--#主菜单-->
<div class="container">
    <ul class="breadcrumb">
        <li><a href="../home/index">首页</a> <span class="divider">></span></li>
        <li><a href="@url">@areacodeStr</a> <span class="divider">></span></li>
        <li>
        @{
            if (product.productCategory != null)
            {
                <span>@product.productCategory.CategoryName</span>
            }
            else
            {
                <span>默认分类</span>
            }
            }<span class="divider">></span></li>
        <li class="active">@product.ProductName</li>
    </ul>
    <!--//////////////////////////////////////////////////////////////////////////////////-->
    <div class="row">
        <div class="span3">
            <!--商家推荐-->
            <div class="title-line title-line-red">
                <h4>
                    商家推荐</h4>
            </div>
            <!--list-->
            <ul class="media-list" id="fiveRecommended">
                 
            </ul>
            <!--#list-->
            <!--#商家推荐-->
            <div>
            </div>
            <!--//////////////////////////////////////////////////////////////////////////////////-->
            <div class="ad-banner-left">
                <!--AD-->
            <!--AD:220x60-->
        @{GoodMan.Tool.Configration.SiteResource.BlockItem adv = GoodMan.Tool.Configration.SiteResource.Block.FirstOrDefault(c => c.Key == "4").Items.FirstOrDefault();}
            @if (adv != null)
            { 
                <a href="@adv.href">
                <img src="@adv.img" alt="@adv.alt" /></a> 
            }
            <!--#AD-->
            </div>
            <!--浏览记录-->
           
            <!--#list-->
            <!--#浏览记录-->
        </div>
        <div class="span9">
            <div class="row-fluid">
                <div class="span5 covers">
                    <!--封面大小:350x240-->
                    @{
                        if (product.Images!=null&&product.Images != "")
                        {
                            var imgStr = product.Images.Split(';')[0];
                            <img style="width:285px;height:190px;" class="cover" src="@imgStr" />
                        }
                        else
                        {
                            <img class="cover" src="" />
                        }
                    }
                    
                    <!--/////////////////////////////////-->
                    <div class="share">
                        <!-- JiaThis Button BEGIN -->
                        <div class="jiathis_style_32x32">
                            <a class="jiathis_button_qzone"></a><a class="jiathis_button_tsina"></a><a class="jiathis_button_tqq">
                            </a><a class="jiathis_button_renren"></a><a class="jiathis_button_kaixin001"></a>
                            <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis"
                                target="_blank"></a><a class="jiathis_counter_style"></a>
                        </div>
                        <!-- JiaThis Button END -->
                    </div>
                </div>
                <div class="span6 details" style="padding-top:0px;">
                <form action="/shop/DirectPurchase" method="post" name="buyproduct" id="buyproduct" >
                    <input type="hidden" id="productid" name="productid" value="@productId" />
                    <input type="hidden" id="areaCode" name="areaCode" value="@areaCode" />
                    <input type="hidden" id="type" name="type" value="@type" />
                        <div class="details" style="vertical-align:top">
                         @{
                             
                             if (type == 2)
                             {
                                  <h4>
                            @product.ProductName<span style='color:#DC143C'>[特别推荐]</span></h4>
                                
                             }
                             else
                             {
                                  <h4>
                            @product.ProductName</h4>
                             }


                             if (areaCode == 1)
                             {

                                 if (Specialpolicy == null)
                                 {  
                                        <div style="margin-bottom:10px;">
                                        <span class="key">价格：</span><span style="font-size:19px;color:#DC143C;font-weight:bold;">&yen;@product.policy.Price</span></div>
                                     <input type="hidden" id="price" name="price" value="@product.policy.Price" />
                                 }
                                 else
                                 {
                                     System.TimeSpan ts = Specialpolicy.ExpireDate.Subtract(DateTime.Now);
                                     double totalmilseconds = (Specialpolicy.ExpireDate - hisDate).TotalSeconds;   
                                    
                                        <div style="margin-bottom:10px;">
                                        <span class="key">价格：</span><span><s>&nbsp;&yen;@product.policy.Price&nbsp;</s></span></div>
                                        <div style="margin-bottom:10px;">
                                        <span class="key">特卖价：</span><span style="font-size:19px;color:#DC143C;font-weight:bold;">&yen;@Specialpolicy.Price</span>
                                        <br /><span id="lefttime" name="lefttime" style="margin-left:84px;margin-bottom:10px;color:#666;font-size:18px;" value="@totalmilseconds"></span></div>
                                    <input type="hidden" id="price" name="price" value="@Specialpolicy.Price" />
                                 }
                             }
                             else if (areaCode == 2)
                             {
                                 decimal discount = 10;
                                 discount = Math.Round(product.policy.Price / product.OriginalPrice * 10, 2);
                                 decimal savemoney = product.OriginalPrice - product.policy.Price;

                                 double totalmilseconds = (product.policy.ExpireDate - hisDate).TotalSeconds;   
                                 
                                    <div style="margin-bottom:10px;">
                                        <span class="key">原价：</span><span><b>&yen;@product.OriginalPrice&nbsp;</b></span></div>
                                        <div style="margin-bottom:10px;">
                                        <span class="key">折扣：</span><span><b>@discount 折</b></span></div>
                                        <div style="margin-bottom:10px;">
                                        <span class="key">团购价：</span><span style="font-size:19px;color:#DC143C;font-weight:bold;">&yen;@product.policy.Price</span>
                                        <br /><span id="lefttime" name="lefttime" style="margin-left:84px; margin-bottom:10px;color:#666;font-size:18px;" value="@totalmilseconds"></span></div>
                                    <input type="hidden" id="price" name="price" value="@product.policy.Price" />
                                    <div style="margin-bottom:10px;">
                                        <span class="key">节省：</span><span><b>&nbsp;&yen;@savemoney&nbsp;</b></span></div>
                             }
                             else if (areaCode == 3)
                             {
                                    <div style="margin-bottom:10px;">
                                        <span class="key">所需积分：</span><span style="font-size:19px;color:#DC143C;font-weight:bold;">@product.Integral 分</span></div>
                                <input type="hidden" id="price" name="price" value="@product.policy.Price" />
                             }
                            }
                         
                        <div  style="margin-bottom:10px;">
                            <span class="key">销量：</span><span><strong>@product.SaleCount</strong> 件</span><span class="muted"> [剩余 @productRemain 件]</span></div>
                        <div  style="margin-bottom:10px;">
                            <span class="key">评价：</span><span><strong>@totalAppraise</strong> 分</span></div>
                        @{
                            if (product.Version != null && product.Version != "")
                            {
                                string[] verArray = product.Version.Split(';');

                                foreach (var item in verArray)
                                {
                                    var verName = item.Split('#')[0];
                                    var verValue = item.Split('#')[1];
                                    @:<div  style="margin-bottom:10px;"><span class="key">@verName：</span><input type="hidden" name="verNames" id="verNames" value="@verName"/><span>
                                    if (verValue != "")
                                    {
                                        string[] verValueArray = verValue.Split('|');
                                        int j = 0;
                                        foreach (var valueItem in verValueArray)
                                        {
                                            if (j != 0 && j % 2 == 0)
                                            {
                                                <br />
                                                 <span style="margin-left:84px;margin-right:8px;"><input type="radio" id="verValues" name="@verName" value="@valueItem"/>@valueItem</span>
                                            }
                                            else
                                            {
                                                if (j == 0)
                                                {
                                                    <span style="margin-right:8px;"><input type="radio" id="verValues" name="@verName" value="@valueItem" checked="checked" />@valueItem</span>
                                                }
                                                else
                                                {
                                                    <span style="margin-right:8px;"><input type="radio" id="verValues" name="@verName" value="@valueItem"/>@valueItem</span>
                                                }
                                            }
                                            j++;
                                        }
                                    }
                                    @:</span></div>
                                }
                            }
                         }
                        <div  style="margin-bottom:10px;margin-top:10px">
                            <span class="key" style="vertical-align:middle">购买数量：</span><span><input type="text" id="buyCount" name="buyCount" onkeyup="changeNum()" maxlength="5" style="height:12px" class="integer topic"
                                value="1" /> 件</span>
                                @{
                                    if (IsUnderstock == false)
                                    {
                                        <span style="color:Red">库存不足,仅剩 @productRemain 件</span>
                                    }
                                }
                                </div>
                        <div id="submitbtn"  style="margin-bottom:10px;">
                            @{
                                var disable="";
                                if (userRole != 1 || productRemain<=0)
                                 {
                                     disable = "disabled=\"disabled\"";
                                 }
                                
                                if (areaCode == 3)
                                {
                                    if (islogin == 1)
                                    {
                                            <button  type="button" onclick="submitform2()" style="margin-left:84px" class="btn btn-danger" @disable><span class="icon-ok icon-white"></span>&nbsp;立即兑换</button>
                                    }
                                    else
                                    {
                                             <a  href="@loginUrl" style="margin-left:84px" class="btn btn-danger"  @disable><span class="icon-ok icon-white"></span>&nbsp;立即兑换</a>
                                    }
                                }
                                else
                                {
                                    if (islogin == 1)
                                    {
                                            <button type="button" onclick="submitform()" class="btn btn-danger"  @disable><span class="icon-ok icon-white"></span>&nbsp;立即购买</button>@Html.Raw("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")<button type="button"  onclick="addcart();return false;" id="topcart" class="btn btn-success"  @disable><span class="icon-shopping-cart icon-white" ></span>&nbsp;加入购物车</button>
                                    }
                                    else
                                    {
                                            <a  href="@loginUrl"  class="btn btn-danger"  @disable><span class="icon-ok icon-white"></span>&nbsp;立即购买</a>@Html.Raw("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")<button type="button" onclick="addcart();return false;" id="topcart" class="btn btn-success"  @disable><span class="icon-shopping-cart icon-white"></span>&nbsp;加入购物车</button>
                                    }
                                }
                                 
                               
                            }
                           
                        </div>
                    </div>
                </form>
                </div>
            </div>
            <div class="clearfix">
            </div>
            <div class="tabbable">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#tab1" data-toggle="tab">商品详情</a></li>
                    <li><a href="#tab2" data-toggle="tab">商品评论</a></li>
                    <li><a href="#tab3" data-toggle="tab">销售记录</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane active" id="tab1" style="overflow:hidden">
                        <p>
                            <!--////////////内容开始/////////////////////-->
                           @Html.Raw(product.Description)
                            <!--////////////内容结束/////////////////////-->
                        </p>
                    </div>
                    <div class="tab-pane" id="tab2">
                        <p>
                            <div class="eval">
                                商品与描述相符<span class="rate">@totalAppraise</span>分@*&nbsp;&nbsp;<span class="muted">[购买商品用户登录可参与评价]</span>*@
                               @* @{
                                    if (IsViewAppriaseBtn == 1)
                                    {
                                        <a  href="javascript: doAppraise()" class="btn btn-danger" style="margin-left:50px;"><span class="icon-edit icon-white">
                                </span>&nbsp;我要评论</a>
                                    }
                                }*@
                                
                            </div>
                            @*@{
                                if (IsViewAppriaseBtn == 1)
                                {   
                             <div id="doAppraise" class="Hide" style="overflow:hidden;display:none">
                        <h4 class="border-bottom-gray red bg-bar">
                            &nbsp;&nbsp;我要评价</h4>
                            <div class="padding-h-10">
                                <form  action="/shop/Appraise" method="post">
                                <input type="hidden" value="@productId" name="pid" />
                               <div class="row-fluid">
                               <div>评分：<span><input type="radio" value="5" checked="checked" name="appraiseGrade"/>非常满意</span>
                               &nbsp;&nbsp;<span><input type="radio" value="4"  name="appraiseGrade"/>满意</span>
                               &nbsp;&nbsp;<span><input type="radio" value="2"  name="appraiseGrade"/>一般</span>
                               &nbsp;&nbsp;<span><input type="radio" value="1"  name="appraiseGrade"/>不满意</span>
                               &nbsp;&nbsp;<span><input type="radio" value="0"  name="appraiseGrade"/>非常不满意</span> 
                               </div>
                               <br />
                               <textarea style="width:99%" name="appraiseContent"></textarea>
                               <br />
                               <div class="text-center"><input type="submit" class="btn btn-danger" value="确定"/></div>
                                </div>
                                </form>
                            </div>
                            </div>
                                }
                            
                        }*@
                             
                                    @{
                                        if (appraiselist != null && appraiselist.Data != null && appraiselist.Data.Any())
                                        {
                                            foreach (var Item in appraiselist.Data)
                                            {
                                                string date = Item.AppraiseDate.ToString("yyyy-MM-dd HH:mm:ss");
                                                 <div class="line-dot">
                                        </div>
                                               <div class="padding-h-10">
                                            <div class="row-fluid">
                                                <div class="span8 muted">
                                                    @Item.user.NickName &nbsp;&nbsp;&nbsp;@date &nbsp;&nbsp;&nbsp;
                                                    @{
                                                if (DelAuthenticate == 1)
                                                {
                                                            <a href="/shop/DelAppraise?id=@Item.Id&type=0&productid=@productId" class="btn btn-danger btn-mini">删除</a>
                                                }
                                                     }
                                                </div>
                                                <div class="span4 text-right">

                                                @{
                                                for (int i = 0; i < Item.AppraiseGrade; i++)
                                                {
                                                        <img src="/content/img/star.png" />
                                                }
                                                  }
                                                     
                                                </div>
                                            </div>
                                            <small>@Item.AppraiseContent
                                            </small>
                                        </div>
                                       
                                            }
                                        }
                                    }
                                     
                                
                            <!--分页-->
                            <div class="pagination text-right" >
                                 <a href="/shop/ProductAppraises?id=@productId" target="_blank">更多评价>></a>
                            </div>
                            <!--#分页-->
                        </p>
                    </div>
                    <div class="tab-pane" id="tab3">
                        
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>
                                            买家
                                        </th>
                                        @{
                                            if(areaCode==3)
                                            {
                                                 <th>
                                            积分
                                        </th>
                                            }
                                            else
                                            {
                                                 <th>
                                            价格
                                        </th>
                                            }
                                        }
                                       
                                        <th>
                                            数量
                                        </th>
                                        <th>
                                            购买时间
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                @{
                                    if (orderDetails != null && orderDetails.Data != null && orderDetails.Data.Any())
                                    {
                                        foreach (var item in orderDetails.Data)
                                        {
                                            string orderdate = item.order.OrderDate.ToString("yyyy-MM-dd HH:mm:ss");
                                            <tr>
                                                <td>
                                                    @item.user.NickName
                                                </td>
                                                @{
                                                    if(areaCode==3)
                                                    {
                                                        double price = Convert.ToDouble(item.Price);
                                                        <td>
                                                        @price
                                                        </td>
                                                    }
                                                    else
                                                    {
                                                    <td>
                                                        @item.Price
                                                    </td>
                                                    }
                                                    }
                                                <td>
                                                    @item.Num
                                                </td>
                                                <td>
                                                    @orderdate
                                                </td>
                                            </tr>
                                        }
                                    }
                                } 
                                </tbody>
                            </table>
                            <!--分页-->
                            <div class="pagination text-right">
                                <a href="/shop/ProductSaleRecords?id=@productId" target="_blank">更多记录>></a>
                            </div>
                            <!--#分页-->
                        
                    </div>
                </div>
            </div>
            
        </div>
    </div>
</div>
@section tail{
    <script type="text/javascript">
        var serverTime =   parseInt('@serverDate')*1000;
        $(function () {
          
           var dateTime = new Date();
             
            var difference = dateTime.getTime() - serverTime;
            
              window.setInterval(function () {
                  $("#lefttime").each(function () {
                      var obj = $(this);
                       
                      var endTime = new Date(parseInt(obj.attr('value')) * 1000);
                      var nowTime = new Date();
                      var nMS = endTime.getTime() - nowTime.getTime() +difference;
                      var myD = Math.floor(nMS / (1000 * 60 * 60 * 24));
                      var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
                      var myM = Math.floor(nMS / (1000 * 60)) % 60;
                      var myS = Math.floor(nMS / 1000) % 60;
                      var myMS = Math.floor(nMS / 100) % 10;
                      if (myD >= 0) {
                          var str = "剩余:"+myD + "天" + myH + "小时" + myM + "分" + myS + "." + myMS + "秒";
                      } else {
                          var str = "团购已结束！";
                          $("#submitbtn").css('display', 'none');
                      }
                      obj.html(str);
                  });
              }, 100);
 

        });

          $(document).ready(function (e) {
              $.post("/Shop/GetFiveRecommended", { type: 0 }, function (response) {
                  $("#fiveRecommended").html(response);
              });
          });
    </script>
    
      
}
